#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
#
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS
VERSION := $(shell cat ./version)


%:
	dh $@ 

override_dh_clean:
	dh_clean
	xbuild /property:Configuration=Release /target:Clean Duplicati.sln
	xbuild /property:Configuration=Release /target:Clean BuildTools/UpdateVersionStamp/UpdateVersionStamp.csproj
	find -type d -name bin | xargs rm -rf
	find -type d -name obj | xargs rm -rf
	find -maxdepth 1 -type d -name build | xargs rm -rf

override_dh_auto_build:
	nuget restore Duplicati.sln

	xbuild /property:Configuration=Release BuildTools/UpdateVersionStamp/UpdateVersionStamp.csproj
	mono BuildTools/UpdateVersionStamp/bin/Release/UpdateVersionStamp.exe --version=$(VERSION)

	xbuild /property:Configuration=Release thirdparty/UnixSupport/UnixSupport.csproj
	cp thirdparty/UnixSupport/bin/Release/UnixSupport.dll thirdparty/UnixSupport/UnixSupport.dll

	xbuild /property:Configuration=Release Duplicati.sln

override_dh_auto_install:
	mkdir build
	mkdir build/bin
	mkdir build/lib
	mkdir build/lib/duplicati
	mkdir build/lib/duplicati/SQLite
	mkdir build/share
	mkdir build/share/applications
	mkdir build/share/pixmaps
	cp Installer/debian/duplicati-launcher.sh build/bin/duplicati
	cp Installer/debian/duplicati-commandline-launcher.sh build/bin/duplicati-cli
	cp Installer/debian/duplicati-server-launcher.sh build/bin/duplicati-server
	cp Duplicati/GUI/Duplicati.GUI.TrayIcon/bin/Release/*.dll build/lib/duplicati/
	cp Duplicati/GUI/Duplicati.GUI.TrayIcon/bin/Release/*.exe build/lib/duplicati/
	cp Duplicati/GUI/Duplicati.GUI.TrayIcon/bin/Release/*.exe.config build/lib/duplicati/
	cp -r Duplicati/GUI/Duplicati.GUI.TrayIcon/bin/Release/SQLite/pinvoke build/lib/duplicati/SQLite/
	cp Duplicati/GUI/Duplicati.GUI.TrayIcon/bin/Release/default_compressed_extensions.txt build/lib/duplicati/
	cp Duplicati/GUI/Duplicati.GUI.TrayIcon/bin/Release/acknowledgements.txt build/lib/duplicati/
	cp Duplicati/GUI/Duplicati.GUI.TrayIcon/bin/Release/changelog.txt build/lib/duplicati/
	cp Duplicati/GUI/Duplicati.GUI.TrayIcon/bin/Release/run-script-example.sh build/lib/duplicati/
	cp Tools/Verification/DuplicatiVerify.py build/lib/duplicati/
	cp -r Duplicati/GUI/Duplicati.GUI.TrayIcon/SVGIcons/ build/lib/duplicati/
	cp -r Duplicati/GUI/Duplicati.GUI.TrayIcon/bin/Release/licenses build/lib/duplicati/
	cp -r Duplicati/GUI/Duplicati.GUI.TrayIcon/bin/Release/lvm-scripts build/lib/duplicati/lvm-scripts
	cp -r Duplicati/Server/webroot build/lib/duplicati/
	if [ -f "oem-app-name.txt" ]; then cp "oem-app-name.txt" build/lib/duplicati; fi
	if [ -f "oem-update-url.txt" ]; then cp "oem-update-url.txt" build/lib/duplicati; fi
	if [ -f "oem-update-key.txt" ]; then cp "oem-update-key.txt" build/lib/duplicati; fi
	if [ -f "oem-update-readme.txt" ]; then cp "oem-update-readme.txt" build/lib/duplicati; fi
	if [ -f "oem-update-installid.txt" ]; then cp "oem-update-installid.txt" build/lib/duplicati; fi
	cp Installer/debian/duplicati.desktop build/share/applications
	cp Installer/debian/duplicati.xpm build/share/pixmaps
	cp Installer/debian/duplicati.png build/share/pixmaps
	cp Installer/debian/duplicati.svg build/share/pixmaps	
	rm -rf build/lib/duplicati/win-tools
	rm -rf build/lib/duplicati/SQLite/win64
	rm -rf build/lib/duplicati/SQLite/win32
	rm -rf build/lib/duplicati/MonoMac.dll
	rm -rf build/lib/duplicati/OSX\ Icons
	rm -rf build/lib/duplicati/OSXTrayHost
	rm -rf build/lib/duplicati/licenses/MonoMac
	rm -rf build/lib/duplicati/licenses/gpg
	find build/lib/duplicati/* -type f | xargs chmod 644
	find build/lib/duplicati/* -type d | xargs chmod 755
	find build/lib/duplicati/* -type f -name \*.exe | xargs chmod 755
	find build/lib/duplicati/* -type f -name \*.sh | xargs chmod 755
	find build/lib/duplicati/* -type f -name \*.py | xargs chmod 755
	dh_install

#override_dh_auto_install:
#	rm -rf Duplicati/GUI/Duplicati.GUI.TrayIcon/bin/Release/*.mdb
#	mkdir ${DESTDIR}/usr/lib/duplicati/
#	cp -R Duplicati/GUI/Duplicati.GUI.TrayIcon/bin/Release/* ${DESTDIR}/usr/lib/duplicati/
#	cp -R Duplicati/Server/webroot ${DESTDIR}/usr/lib/duplicati/

override_dh_systemd_enable:
	dh_systemd_enable --no-enable
